package com.driver.pojo.response;

import com.driver.enums.ResponseCode;
import lombok.Getter;
import lombok.Setter;

/**
 * @Description 全局返回给前端的对象
 */
@Getter
@Setter
public class R<T> {

    private Integer code; // 状态码
    private String message; // 消息
    private T data; // 数据

    public R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public R(Integer code, String message) {
        this(code, message, null);
    }

    public static<T> R<T> success(T data) {
        return new R<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    public static<T> R<T> success() {
        return success(null);
    }

    public static<T> R<T> fail(ResponseCode responseCode, String message) {
        return new R<>(responseCode.getCode(), message);
    }

    public static <T> R<T> fail(ResponseCode responseCode) {
        return fail(responseCode, responseCode.getMessage());
    }

    public static<T> R<T> fail(String message) {
        return new R<>(ResponseCode.FAIL.getCode(), message);
    }

}
